import _ from "lodash";

export function merge(state, {key, data}) {
    const oldData = state[key];
    state[key] = {...oldData, ...data};
}

export function mergeState(state, payload) {
    _.each(payload, (data, key) => merge(state, {key, data}));
}

export function update(state, {key, data}) {
    state[key] = {...data};
}

export function updateState(state, payload) {
    _.each(payload, (data, key) => update(state, {key, data}));
}

export function invalidMessage(res) {
    const title = res.message;
    const messages = [];
    _.each(res.errors, (value, key) => {
        messages.push(`<li>${key}:${value.join(",")}</li>`);
    });
    return {title, message: `<ul>${messages.join("")}</ul>`}
}
